package appapi.service.impl;

import appapi.Global.BusinessException;
import appapi.Vo.AreaVo;
import appapi.dao.AreaMapper;
import appapi.dto.basic.GetAreaDto;
import appapi.service.IAreaService;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author djq
 * @date 2023/5/23 9:11
 * @description:
 */
@Component
public class AreaServiceImpl implements IAreaService {
    @Resource
    private AreaMapper areaMapper;

    @Override
    public List<AreaVo> SelectList() {

        GetAreaDto dto = new GetAreaDto();
        dto.setPid(0);
        List<AreaVo> areaVos = selectArea(dto);

        return areaVos;
    }

    @Override
    public List<AreaVo> SelectList(GetAreaDto dto) {
        if (dto.getPid()==null&& ObjectUtils.isEmpty(dto.getName())){
            throw new BusinessException(-1,"参数不能为空!");
        }
        return areaMapper.selectByPid(dto);
    }


    /**
     * 递归查询省市区
     * @param pid
     * @return
     */
    public List<AreaVo> selectArea(GetAreaDto pid) {
        List<AreaVo> areaVos = areaMapper.selectByPid(pid);
        if (areaVos == null) return null;
        areaVos.forEach((area) -> {
            GetAreaDto id = new GetAreaDto();
            id.setPid(area.getId());
            area.setChildren(selectArea(id));
        });
        return areaVos;
    }
}
